//
//  EBHCountDownView.m
//  EBHDoctor
//
//  Created by 黄德玉 on 2019/11/6.
//  Copyright © 2019 DCloud. All rights reserved.
//

#import "EBHCountDownView.h"
#import "Masonry.h"

@implementation EBHCountDownView


- (void)dealloc{
    [self.timer invalidate];
    self.timer = nil;
}

- (instancetype)initWithCountDown:(NSInteger)countDown{
    if (self = [super init]) {
        self.countDown = countDown;
        if (self.countDown > 0) {
            NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970] * 1000;
            self.countDown = self.countDown - timeInterval;
            self.countDown = self.countDown / 1000;
            self.countDown = self.countDown > 0 ? self.countDown : 0;
        }
        self.bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bgBtn setBackgroundImage:[UIImage imageNamed:@"clock"] forState:UIControlStateNormal];
        self.bgBtn.userInteractionEnabled = NO;
        [self addSubview:self.bgBtn];
        [self.bgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
            make.width.equalTo(@144);
            make.height.equalTo(@41);
        }];
        
        self.desLbl = [UILabel new];
        self.desLbl.frame = CGRectMake(238,386.5,95,24.5);
        self.desLbl.numberOfLines = 0;
        [self addSubview:self.desLbl];
        self.desLbl.textAlignment = NSTextAlignmentCenter;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"距离结束时间还剩" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]}];
        self.desLbl.attributedText = string;
        [self.desLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(7);
            make.top.equalTo(self).offset(6);
        }];
        self.countDownLbl = [UILabel new];
        self.countDownLbl.font = [UIFont boldSystemFontOfSize:12];
        self.countDownLbl.textColor = [UIColor whiteColor];
        self.countDownLbl.text = @"00:00:00";
        [self addSubview:self.countDownLbl];
        [self.countDownLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.desLbl);
            make.top.equalTo(self.desLbl.mas_bottom);
        }];
        __weak typeof(self)weakSelf = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES
                                         block:^(NSTimer * _Nonnull timer) {
          if (weakSelf.countDown == 0) {
//              [weakSelf.timer invalidate];
//              weakSelf.timer = nil;
              return;
          }
          weakSelf.countDown--;
          weakSelf.countDownLbl.text = [weakSelf getMMSSFromSS:[@(weakSelf.countDown) stringValue]];
       }];
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        [self.timer fire];
        
        self.logoView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"clockLogo"]];
        [self addSubview:self.logoView];
        [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self);
        }];
    }
    return self;
}

- (void)updateCountDown:(NSInteger)countDown{
     self.countDown = countDown;
   if (self.countDown > 0) {
       NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970] * 1000;
       self.countDown = self.countDown - timeInterval;
       self.countDown = self.countDown / 1000;
       self.countDown = self.countDown > 0 ? self.countDown : 0;
   }
}

-(NSString *)getMMSSFromSS:(NSString *)totalTime{

    NSInteger seconds = [totalTime integerValue];

    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];

    return format_time;

}

@end
